package com.gdit.revenue.controller;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.gdit.revenue.base.result.Results;
import com.gdit.revenue.model.User;
import com.gdit.revenue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
    //这个就开始AOP面向切面编程
    private static final Log log = LogFactory.getLog(UserController.class);

    @Autowired
    UserService userServiceImpl;
    @RequestMapping("/userlist")
    @ResponseBody   //解析Json数据
    public Results userlist(int page, int limit){
        log.warn("用户调用了/user/userlist");    //日志
        int start = (page-1)*limit;     //第几个数据开始展示
        return userServiceImpl.getAllUsers(start,limit);
    }

    @RequestMapping("/adduser")
    public String adduser(){
        return "/user/adduser";
    }

    @RequestMapping("/updateuser")
    public String updateuser(){
        return "/user/updateuser";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Results delete(int userid){
        return userServiceImpl.delete(userid);
    }

    @PostMapping("/addUser")
    @ResponseBody
    public int addUser(@RequestBody User user){
        userServiceImpl.addUser(user);
        return 0;
    }

    @PostMapping("/updateUser")
    @ResponseBody
    public Results updateUser(@RequestBody User user){
        return userServiceImpl.updateUser(user);
    }


}
